package com.yuanheng100.ems4j.service.handler;

import com.yuanheng100.ems4j.common.constant.DingIsHide;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by wangguangshuo on 2016/12/15.
 */
public class DingIsHideTypeHandler implements TypeHandler<DingIsHide>
{
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, DingIsHide dingIsHide, JdbcType jdbcType) throws SQLException
    {
        if (dingIsHide != null)
        {
            preparedStatement.setShort(i, dingIsHide.code);
        } else
        {
            preparedStatement.setObject(i, null);
        }
    }

    @Override
    public DingIsHide getResult(ResultSet resultSet, String s) throws SQLException
    {
        Short code = resultSet.getShort(s);
        return getDingIsHide(code);
    }

    @Override
    public DingIsHide getResult(ResultSet resultSet, int i) throws SQLException
    {
        Short code = resultSet.getShort(i);
        return getDingIsHide(code);
    }

    @Override
    public DingIsHide getResult(CallableStatement callableStatement, int i) throws SQLException
    {
        Short code = callableStatement.getShort(i);
        return getDingIsHide(code);
    }


    private DingIsHide getDingIsHide(Short code)
    {
        if (code != null)
        {
            return DingIsHide.dingMap.get(code);
        }
        return null;
    }

}
